#include "stdafx.h"
#include "ZAManagerImpl.h"

#include "JSGlobalFunction.h"
#include "JSObj_BaseInfo.h"
#include "JSObj_WebsiteInfo.h"

CZAManagerImpl::CZAManagerImpl()
	: m_pWeb(NULL)
{
	CoInitialize(NULL);
}

CZAManagerImpl::~CZAManagerImpl()
{
//	m_pWeb->DestroyWindow();
//	delete m_pWeb;
//	DestroyWindow();
	CoUninitialize();
}

void CZAManagerImpl::Release()
{
	delete this;
}

BOOL CZAManagerImpl::Init(PZAMANAGERINIT pInit)
{
	if (m_pWeb)
		return TRUE;

	CString str = _T("http://www.51mza.cn/ZAManager/");
	str = _T("E:\\svn360\\za\\wwwroot\\ZAManager\\index.htm");

	m_pWeb = new CWebCtrl;
	BOOL b = CWnd::CreateEx (NULL, AfxRegisterWndClass(0), NULL, WS_OVERLAPPED, 0, 0, 100, 100, NULL, NULL) ;
	b = m_pWeb->Create(NULL, NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 0, 0), this, 1234);
	m_pWeb->NavigateEx(str);

	GFINIT init = {0};
	if (pInit)
	{
		init.pfnAddLog = pInit->pfnAddLog;
		init.pContext = pInit->pContext;
	}

	CComPtr<IDispatch> spDisp;
	CreateFunDispInstance(m_pWeb->GetWebBrowser(), &spDisp, &init);
	m_pWeb->SetExternal(spDisp);

	return TRUE;
}

BOOL CZAManagerImpl::GetBaseInfo( PZAMANAGERBASEINFO pInfo )
{
	if (NULL==m_pWeb || NULL==pInfo)
		return FALSE;

	BOOL bRet = FALSE;
	_variant_t vRet = ExecuteScript(m_pWeb->GetWebBrowser(), _T("DoGetBaseInfo"), 0l);
	if (VT_DISPATCH==vRet.vt)
	{
		CJSObj_BaseInfo baseInfo(m_pWeb->GetWebBrowser(), vRet.pdispVal);

		pInfo->bstrTitle = baseInfo.m_bstrTitle.copy();
		pInfo->bstrZAName = baseInfo.m_bstrZAName.copy();
		pInfo->bstrZASex = baseInfo.m_bstrZASex.copy();
		pInfo->nZAType = baseInfo.m_lZAType;
		pInfo->bstrPrice = baseInfo.m_bstrPrice.copy();
		pInfo->bstrLocalPhoto = baseInfo.m_bstrLocalPhoto.copy();
		pInfo->bstrWebPhoto = baseInfo.m_bstrWebPhoto.copy();
		pInfo->bstrIntroduction = baseInfo.m_bstrIntroduction.copy();
		
		pInfo->bstrAYName = baseInfo.m_bstrAYName.copy();
		pInfo->bstrUserName = baseInfo.m_bstrUserName.copy();
		pInfo->bstrPhone = baseInfo.m_bstrPhone.copy();
		pInfo->bstrMobile = baseInfo.m_bstrMobile.copy();
		pInfo->bstrAddress = baseInfo.m_bstrAddress.copy();
		pInfo->bstrWebsite = baseInfo.m_bstrWebsite.copy();
		pInfo->bstrContact = baseInfo.m_bstrContact.copy();

		bRet = TRUE;
	}

	return bRet;
}

void CZAManagerImpl::SetBaseInfo( PZAMANAGERBASEINFO pInfo )
{
	if (NULL==m_pWeb || NULL==pInfo)
		return;
	
	CJSObj_BaseInfo baseInfo(m_pWeb->GetWebBrowser());

	baseInfo.m_bstrTitle = pInfo->bstrTitle;
	baseInfo.m_bstrZAName = pInfo->bstrZAName;
	baseInfo.m_bstrZASex = pInfo->bstrZASex;
	baseInfo.m_lZAType = pInfo->nZAType;
	baseInfo.m_bstrPrice = pInfo->bstrPrice;
	baseInfo.m_bstrLocalPhoto = pInfo->bstrLocalPhoto;
	baseInfo.m_bstrWebPhoto = pInfo->bstrWebPhoto;
	baseInfo.m_bstrIntroduction = pInfo->bstrIntroduction;
	
	baseInfo.m_bstrAYName = pInfo->bstrAYName;
	baseInfo.m_bstrUserName = pInfo->bstrUserName;
	baseInfo.m_bstrPhone = pInfo->bstrPhone;
	baseInfo.m_bstrMobile = pInfo->bstrMobile;
	baseInfo.m_bstrAddress = pInfo->bstrAddress;
	baseInfo.m_bstrWebsite = pInfo->bstrWebsite;
	baseInfo.m_bstrContact = pInfo->bstrContact;

	baseInfo.SetAll();
	_variant_t vRet = ExecuteScript(m_pWeb->GetWebBrowser(), _T("DoSetBaseInfo"), baseInfo.GetDispatch());
}

void CZAManagerImpl::FreeBaseInfo( PZAMANAGERBASEINFO pInfo )
{
	if (NULL==pInfo)
		return;

	::SysFreeString(pInfo->bstrTitle);

	::SysFreeString(pInfo->bstrZAName);
	::SysFreeString(pInfo->bstrZASex);
	::SysFreeString(pInfo->bstrPrice);
	::SysFreeString(pInfo->bstrLocalPhoto);
	::SysFreeString(pInfo->bstrWebPhoto);
	::SysFreeString(pInfo->bstrIntroduction);
	
	::SysFreeString(pInfo->bstrAYName);
	::SysFreeString(pInfo->bstrUserName);
	::SysFreeString(pInfo->bstrPhone);
	::SysFreeString(pInfo->bstrMobile);
	::SysFreeString(pInfo->bstrAddress);
	::SysFreeString(pInfo->bstrWebsite);
	::SysFreeString(pInfo->bstrContact);

	ZeroMemory(pInfo, sizeof(ZAMANAGERBASEINFO));
}

UINT CZAManagerImpl::GetWebsiteInfoCount()
{
	return (long)ExecuteScript(m_pWeb->GetWebBrowser(), _T("DoGetWebsiteInfoCount"), 0l);
}

BOOL CZAManagerImpl::GetWebsiteInfo( PZAMANAGERWEBSITEINFO pArr, UINT nCount )
{
	if (NULL==m_pWeb || NULL==pArr || 0==nCount)
		return FALSE;

	BOOL bRet = FALSE;
	_variant_t vRet = ExecuteScript(m_pWeb->GetWebBrowser(), _T("DoGetWebsiteInfo"), 0l);
	if (VT_DISPATCH==vRet.vt)
	{
		bRet = TRUE;

		CScriptObject infos(m_pWeb->GetWebBrowser(), vRet.pdispVal);
		std::deque<CComBSTR> lsMems = infos.GetAllMembers();
		if (lsMems.size() < nCount)
			nCount = lsMems.size();

		UINT nAddCount = 0;
		for (UINT i = 0; i < lsMems.size(); i++)
		{
			CComPtr<IDispatch> spDisp;
			if (FALSE==infos.GetValue(lsMems[i], &spDisp))
				continue;

			CJSObj_WebsiteInfo info(m_pWeb->GetWebBrowser(), spDisp);

			pArr[i].bstrWebsiteID = info.m_bstrWebsiteID.copy();
			pArr[i].bstrWebsiteName = info.m_bstrWebsiteName.copy();
			pArr[i].bstrUrl = info.m_bstrUrl.copy();

			pArr[i].bstrUserName = info.m_bstrUserName.copy();
			pArr[i].bstrPassWord = info.m_bstrPassWord.copy();

			pArr[i].bChecked = (VARIANT_FALSE!=info.m_vbChecked);
			pArr[i].bNeedPost = (VARIANT_FALSE!=info.m_vbNeedPost);
			pArr[i].bstrPostUrl = info.m_bstrPostUrl.copy();

			pArr[i].bNeedIdentifyingCode = (VARIANT_FALSE!=info.m_vbNeedIdentifyingCode);
			pArr[i].bstrLocalIDCPic = info.m_bstrLocalIDCPic.copy();
			pArr[i].bstrIDCInput = info.m_bstrIDCInput.copy();

			nAddCount++;
			if (nAddCount>=nCount)
				break;
		}
	}
	return bRet;
}

BOOL CZAManagerImpl::CheckWebsiteInfo()
{
	_variant_t vRet = ExecuteScript(m_pWeb->GetWebBrowser(), _T("DoCheckWebsiteInfo"), 0l);
	if (0l==(long)vRet)
		return TRUE;
	return FALSE;
}

BOOL CZAManagerImpl::PostWebsiteInfo()
{
	_variant_t vRet = ExecuteScript(m_pWeb->GetWebBrowser(), _T("DoPostWebsiteInfo"), 0l);
	if (0l==(long)vRet)
		return TRUE;
	return FALSE;
}

void CZAManagerImpl::FreeWebsiteInfo( PZAMANAGERWEBSITEINFO pArr, UINT nCount )
{
	if (0==nCount || NULL==pArr)
		return;

	for (UINT i = 0; i < nCount; i++)
	{
		::SysFreeString(pArr[i].bstrWebsiteID);
		::SysFreeString(pArr[i].bstrWebsiteName);
		::SysFreeString(pArr[i].bstrUrl);

		::SysFreeString(pArr[i].bstrUserName);
		::SysFreeString(pArr[i].bstrPassWord);

		::SysFreeString(pArr[i].bstrPostUrl);
		
		::SysFreeString(pArr[i].bstrLocalIDCPic);
		::SysFreeString(pArr[i].bstrIDCInput);
	}

	ZeroMemory(pArr, sizeof(ZAMANAGERWEBSITEINFO)*nCount);
}
